package com.factory.test.receiver;

import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.factory.test.base.MyApplication;
import com.yecontech.sdk.base.YTSdkService;

import java.io.File;
import java.util.List;

/**
 * Created by Administrator on 2019/3/7.
 */

public class UsbReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Log.d("TAG", "onReceive1111: ");
        if (action.equals(Intent.ACTION_MEDIA_MOUNTED)){

            YTSdkService sdkService = MyApplication.getSdkService(context);
            String usbRootPath =  intent.getData().getPath();
            if(usbRootPath == null){
                Log.e("usb", "USB 路径获取失败...");
                return;
            }
            Log.e("usb", "USB 路径获取成功..."+usbRootPath);
            File file = new File(usbRootPath, "YXTest.txt");
            if(file.exists() && !isAppOnForeground(context)){
//                ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
//                String pn = am.getRunningTasks(1).get(0).topActivity.getPackageName();
//                if(context.getPackageName().equals(pn)){
//                    Log.i("usb", "当前应用在前面：" + pn);
//                }

//                Intent intent1 = new Intent(Intent.ACTION_MAIN);
//                intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                intent1.addCategory(Intent.CATEGORY_LAUNCHER);
//                intent1.setComponent(new ComponentName(context.getPackageName(), "com.factory.test.main.MainActivity"));
//                context.startActivity(intent1);


                try {
                    Intent intent1 = new Intent("com.factory.test.main.MainActivity");
                    intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent1);
                } catch (Exception e) {
                    Log.e("usb", "工厂测试启动失败...", e);
                }
            }
        }
    }

    /**
     * 判断当前应用是在前台, 还是在后台
     * @param context
     * @return
     */
    public boolean isAppOnForeground(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        boolean isOnForground = false;
        List<ActivityManager.RunningAppProcessInfo> runnings = am.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo running : runnings) {
            if (running.processName.equals(context.getPackageName())) {
                if (running.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
                        || running.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE) {
                    //前台显示...
                    Log.i("config", "前台显示");
                    isOnForground = true;
                } else {
                    //后台显示...
                    Log.i("config", "后台显示");
                    isOnForground = false;
                }
                break;
            }
        }
//        String currentPackageName = "";
//        if (am.getRunningTasks(1).size() > 0) {
//            ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
//            currentPackageName = cn.getPackageName();
//        }
        return isOnForground;
    }
}
